import { BlockComponentTypes, GameMode } from "@minecraft/server";
import { Dimensions } from "@sapi-game/constants";
import { Timer } from "@sapi-game/gameComponent/common/timer/timer";
import { BlockInteractionBlocker } from "@sapi-game/gameComponent/player/blockInteractionBlocker";
import { PlayerGroupSet } from "@sapi-game/gamePlayer/groupSet";
import { HideAndSeekDaemon } from "./components/daemon";
import { HideAndSeekEffectActuator } from "./components/itemEffectActuator";
import { HideAndSeekItemsManager } from "./components/items";
import { HideAndSeekRespawn } from "./components/respawn";
import { SeekerDrawer } from "./components/seekerDrawer";
import { SeekerEquipment } from "./components/seekerEquip";
import { HideAndSeekEndState } from "./endState";
import { HideAndSeekLuckyItems } from "./luckyItems";
import { HideAndSeekModule } from "./module";
import { HideAndSeekRole } from "./player";

export class HideAndSeekMainState extends HideAndSeekModule.State {
    override onEnter(): void {
        this.init();

        const groupSet = new PlayerGroupSet([this.context.players]);
        this.addComponent(SeekerEquipment)
            .addComponent(SeekerDrawer)
            .addComponent(BlockInteractionBlocker, {
                groupSet: groupSet,
                blockComponentType: BlockComponentTypes.Inventory,
                showMessage: false,
            })
            .addComponent(Timer, { initialTime: this.context.config.time });

        this.addComponents(this.context.mode.components);
    }

    //玩家初始化
    init() {
        this.context.players.forEach((p) => {
            p.player.teleport(this.context.map.spawnPoint);
            p.player.setGameMode(GameMode.Adventure);
            p.player.runCommand("effect @s clear");
            p.clear();
            p.role = HideAndSeekRole.Hider; //默认为躲藏者
            p.player.setSpawnPoint({
                dimension: Dimensions.Overworld,
                ...this.context.map.spawnPoint,
            });
        });
    }

    /**抽追捕者 */
    drawSeeker() {
        this.getComponent(SeekerDrawer).drawSeeker();
    }

    start() {
        this.addComponent(HideAndSeekDaemon)
            .addComponent(HideAndSeekRespawn)
            .addComponent(HideAndSeekEffectActuator)
            .addComponent(HideAndSeekItemsManager, {
                interval: 60,
                itemPoints: this.context.map.itemPoints,
                items: HideAndSeekLuckyItems,
            });
        this.getComponent(Timer).start();
    }

    end() {
        const allPlayers = this.context.players.getAll();
        const hasHider = allPlayers.some(
            (p) => p.alive && p.isValid && p.role == HideAndSeekRole.Hider
        );
        const hasSeeker = allPlayers.some(
            (p) => p.alive && p.isValid && p.role == HideAndSeekRole.Seeker
        );
        let winner: "hider" | "seeker" | null;
        if (hasHider) {
            //有躲藏者，一定是躲藏者胜利
            winner = "hider";
        } else if (hasSeeker) {
            //没躲藏者，还得看有没有抓捕者
            winner = "seeker";
        } else {
            //都没有则没人胜利
            winner = null;
        }
        this.transitionTo(HideAndSeekEndState, { winner: winner });
    }
}
